package com.xiaobaibai.security.master_login.dao;

import com.xiaobaibai.service.IUserAdminService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

//@Component
public class LoginUserDetailService implements UserDetailsService {

    private IUserAdminService userAdminService;

    public LoginUserDetailService(IUserAdminService userAdminService) {
        this.userAdminService = userAdminService;
    }

    @Override
    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {

        String password=userAdminService.getPasswordByUserId(userId);

        if(password==null)
            throw new RuntimeException("用户不存在");

        System.out.println(Thread.currentThread().getName());

        //四个boolean 可用  没过期  密码没过期  是否没被锁定
        return new User(userId,password,true,true,
                true,true,
                //作用:把一个带有','的String 转为权限的List
                AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
    }

}
